<?php

include 'php-ofc-library/open-flash-chart.php';
require_once 'bootstrap.php';


//////////////////////////////start of calling database//////////////////////////

$dbstat = new DbStat();

//creating an array for the past 24hrs breakdown starting from 0-1 hr, 1-2 hr ....
$dbss = array();
date_default_timezone_set("Asia/Singapore");
for($i = 0; $i < 24; $i++) {
	$dbss[] = $dbstat->getDuration(date('YmdHms',time()-((24-$i)*60*60)), date('YmdHms', time()-((23-$i)*60*60)));
} 

$averages = array();
//var_dump($dbss);
foreach ($dbss as $key => $dbs1) {
	$averages[$key] = 0.0; // initialize
	
	if($dbs1) {
		// if $dbs1 is not empty
		foreach($dbs1 as $res) {
			$averages[$key] += $res['PCT_FREE_MEM'];
		}
		$averages[$key] /= sizeof($dbs1); // get average
	}
}
//var_dump($averages);

// label need to change accordingly to user input x value -> default 60 mins(1hr block)
# Define the data array: Label, the 1 data set.
# Time,  PCT_FREE_MEM -> default 15
//This is the data which we need to plot the graph
$data = array();
for ($i = 0; $i < 24; $i++) {
	//$data[] = array( $i, $averages['PCT_FREE_MEM'] );
	$data[] = floatval($averages[$i]);
}

//var_dump($data);

/*end of calling database*/

/*
$data = array();
$max = 0;
while($row = mysql_fetch_array($result))
{
  $data[] = intval($row['carForSaleID']);
}
*/





///// start of graph //////
$title = new title('Percentage of free memory (%)' );

$y = new y_axis(); 
$y->set_range( 0, 100, 10 );
$x = new x_axis(); 
$x->set_range( 1, 24, 1 );

$x_legend = new x_legend( 'no. of hours' );
$x_legend->set_style( '{font-size: 16px; color: #778877}' );
$y_legend = new y_legend( 'percentage (%)' );
$y_legend->set_style( '{font-size: 16px; color: #778877}' );



$bar = new bar();
$bar->set_values($data);
$bar->set_colour ('ff0000');
$bar->set_key( 'healthy', 12 );



$chart = new open_flash_chart();
$chart->set_title( $title );
$chart->add_element( $bar );
$chart->set_x_axis( $x );
$chart->set_y_axis( $y );
$chart->set_x_legend( $x_legend );
$chart->set_y_legend( $y_legend );

                    
//echo $chart->toString();
echo $chart->toPrettyString();


?>